package com.gxuwz.leave.servlet;

import com.gxuwz.leave.context.AppContext;
import com.gxuwz.leave.entity.Class;
import com.gxuwz.leave.entity.Course;
import com.gxuwz.leave.service.ClassService;
import com.gxuwz.leave.service.CourseService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "courseServlet", urlPatterns = {"/course"})
public class CourseServlet extends BaseHttpServlet{
    private final static String VIEW_ADD = "/WEB-INF/views/course/course_add.jsp";

    private final static String VIEW_EDIT = "/WEB-INF/views/course/course_edit.jsp";

    private final static String VIEW_LIST = "/WEB-INF/views/course/course_list.jsp";
    private final static String ACTION_LIST = "/course?action=list";
    private CourseService courseService;

    private ClassService classService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");

        String view = ErrorPage_500;
        try{
            switch (action){
                //导向新增页面
                case "input":
                    view = VIEW_ADD;
                    Course course = new Course();
                    List<Class> classList = classService.list(null);
                    System.out.println("查询到的班名有："+classList);
                    req.setAttribute("course", course);
                    req.setAttribute("classList", classList);
                    break;
                //提交新增
                case "add":
                    course = new Course();
                    course.setCourseID(req.getParameter("courseID"));
                    course.setClassID(req.getParameter("classID"));
                    course.setCourseName(req.getParameter("courseName"));
                    course.setYear(req.getParameter("year"));
                    course.setTerm(req.getParameter("term"));
                    course.setHour(req.getParameter("hour"));
                    courseService.addCourse(course);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //编辑操作时，获取那一行的数据
                case "get":
                    String courseID = req.getParameter("courseID");
                    course = courseService.findCourse(courseID);
                    classList = new ArrayList<>();
                    classList = classService.list(null);
                    view = VIEW_EDIT;
                    req.setAttribute("course",course);
                    req.setAttribute("classList", classList);
                    break;
                //提交编辑操作
                case "edit":
                    course = new Course();
                    course.setCourseID(req.getParameter("courseID"));
                    course.setClassID(req.getParameter("classID"));
                    course.setCourseName(req.getParameter("courseName"));
                    course.setYear(req.getParameter("year"));
                    course.setTerm(req.getParameter("term"));
                    course.setHour(req.getParameter("hour"));
                    courseService.editCourse(course);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //搜索
                case "search":
                    String keywords = req.getParameter("keywords");
                    course = new Course();
                    course.setCourseID(keywords);
                    List<Course> list = courseService.list(course);
                    if(list.isEmpty()){
                        course = new Course();
                        course.setCourseName(keywords);
                        list = courseService.list(course);
                    }
                    view = VIEW_LIST;
                    req.setAttribute("list", list);
                    break;
                //删除操作
                case "delete":
                    courseID = req.getParameter("courseID");
                    courseService.deleteCourse(courseID);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                case "list":
                    System.out.println("查询");
                    List<Course> courseList = courseService.list(null);
                    System.out.println("查询到的课程有："+courseList);
                    view = VIEW_LIST;
                    req.setAttribute("list", courseList);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            req.setAttribute("message", e.getMessage());
            req.setAttribute("operation", "failure");
            view = ACTION_LIST;
        }
        super.doDispatch(req,resp,view);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        courseService = AppContext.getBean(CourseService.class);
        classService = AppContext.getBean(ClassService.class);
    }
}
